package com.md.api.common.utils;

public class IDCardValidator {

    public static boolean isValidChineseID(String id) {
        if (id == null || (id.length() != 18)) {
            return false;
        }

        char[] idArray = id.toCharArray();
        int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] checkCode = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        int sum = 0;

        for (int i = 0; i < idArray.length - 1; i++) {
            int num = Integer.parseInt(String.valueOf(idArray[i]));
            if (num < 0 || num > 9) {
                return false;
            }
            sum += num * weight[i];
        }

        sum = sum % 11;
        char lastChar = idArray[17];
        return lastChar == checkCode[sum];
    }
}
